package io.renren.modules.distribution.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import io.renren.common.utils.ParamUtils;
import io.renren.common.utils.R;
import io.renren.htby.utils.SocketUtils;
import io.renren.modules.distribution.service.DisService;
import io.renren.socket.server.DisConfigNIOServer;

@Service("disService")
public class DisServiceImpl implements DisService {
	
	private static Logger logger = LoggerFactory.getLogger(DisServiceImpl.class);

	@Override
	public R sendDisTopic(Map<String, Object> params) {
		try {
			// 获得 前端 参数 topic
			String topic = (String) params.get("disTopic");
			// 获取 已经创建 的连接 nio服务端
			DisConfigNIOServer server = (DisConfigNIOServer) SocketUtils.ServersMap.get(SocketUtils.DIS_SERVER_KEY);
			// 发送消息
			server.write(topic.getBytes());
		} catch (Exception e) {
			logger.error("发送失败。");
			return R.ok().put("result", ParamUtils.TIP_FALSE).put("msg", "发送失败。");
		}
		logger.info("发送成功。");
		return R.ok().put("result", ParamUtils.TIP_TRUE).put("msg", "发送成功。");
	}

}
